package mytest.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import mytest.util.RequestUtil;
import mytest.util.Response;
import test.service.ServiceTestCase;
import test.testcase.TestCaseApi;
import test.testcase.dao.TestCaseApiDao;
import test.testcase.dao.TestCaseDao;
import test.utils.JsonResult;

@Controller @RequestMapping(value = "/testcaseapi", produces = "application/json; charset=utf-8")
@CrossOrigin
public class testcaseapi {
	
	@RequestMapping(value = "/addapi", method = RequestMethod.POST)
	public @ResponseBody String addApi(@RequestBody String body, HttpServletRequest request) throws UnsupportedEncodingException {
		TestCaseApi api = new TestCaseApi();
		JsonResult result = JsonResult.parse(URLDecoder.decode(body, "UTF-8"));
		String url = result.read("api").toString();
		//判断是否重复
		JsonResult result2 = TestCaseDao.getApi(url, result.read("method").toString());
		if (result2.read("data").hasArrayItems()) {
			Response response = new Response();
			response.setRetCodeFalse();
			response.setRetMessage("接口地址重复");
			return response.toString();
		}
		api.setUrl(url);
		api.setContent(result.read("desc").toString());
		api.setContenttypeid(result.read("contenttype").toString());
		api.setMethod(result.read("method").toString());
		String projectid= RequestUtil.getProjectId(request);
		api.setProjectid(projectid);
		if (projectid == null || projectid.equals("")) {
			Response response = new Response();
			response.setRetCodeFalse();
			response.setRetMessage("请选择项目");
			return response.toString();
		}
		api.setUserid(RequestUtil.getUserId(request));
		return TestCaseDao.addApi(api).toResponse();
	}
	
	@RequestMapping(value = "/getapilist")
	public @ResponseBody String getApiList(HttpServletRequest request) {
		return TestCaseDao.getApilist(RequestUtil.getProjectId(request), RequestUtil.getStartPage(request), RequestUtil.getEndPage(request)).toResponse();
	}
	
	@RequestMapping(value = "/delapi/{id}")
	public @ResponseBody String delapi(@PathVariable String id) {
		if (ServiceTestCase.isJobContainCase(id)) {
			Response response = new Response();
			response.setRetCodeFalse();
			response.setRetMessage("有测试用例在使用该接口，不能删除");
			return response.toString();
		}else {
			return TestCaseApiDao.delById(id);
		}
	}
	
	@RequestMapping(value = "/apiinfo/{id}")
	public @ResponseBody String getApiInfo(@PathVariable String id) {
		return TestCaseDao.getApiInfo(id).toResponse();
	}
	
	@RequestMapping(value = "/saveapi/{id}", method = RequestMethod.POST)
	public @ResponseBody String saveApi(HttpServletRequest request, @RequestBody String body, @PathVariable String id) throws UnsupportedEncodingException {
		TestCaseApi api = new TestCaseApi();
		JsonResult result = JsonResult.parse(URLDecoder.decode(body, "UTF-8"));
		//判断是否重复
		JsonResult result2 = TestCaseDao.getApi(result.read("api").toString(), result.read("method").toString());
		if (result2.read("data").hasArrayItems()) {
			Response response = new Response();
			response.setRetCodeFalse();
			response.setRetMessage("接口地址重复");
			return response.toString();
		}
		//判断是否重复
		api.setUrl(result.read("api").toString());
		api.setContent(result.read("desc").toString());
		api.setContenttypeid(result.read("contenttype").toString());
		api.setMethod(result.read("method").toString());
		api.setProjectid(RequestUtil.getProjectId(request));
		return TestCaseDao.updateApi(id, api).toResponse();
	}
	
}
